﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quản_Lý_Thư_Viện
{
    public partial class frmMuonSach : Form
    {
        public frmMuonSach()
        {
            InitializeComponent();
        }


        CT_PTScontrol ptsctrl = new CT_PTScontrol();
        CTPMcontrol pmctrl = new CTPMcontrol();
        DocgiaControl control = new DocgiaControl();
        DataTable dt = new DataTable();
        DocgiaInfo info = new DocgiaInfo();
        public static string madg;
        public static string masach;
        public static int sosachmuon;

        private void frmMuonsach_Load(object sender, EventArgs e)
        {
           // frmChinh frm = new frmChinh();

            dgvMuonSach.DataSource = frmChinh.dtSach;
        }
        private void laythongtindocgia(ref DocgiaInfo info)
        {
            
            info.Madocgia = txtMaDG.Text;

        }

        private void btnKiemTra_Click(object sender, EventArgs e)
        {
            DataTable dt1 = new DataTable();
            DataTable dt2 = new DataTable();
            DataRow dr1;
            DataRow dr2;
            dt1 = pmctrl.sosachdgdamuon();
            dt2 = ptsctrl.sosachdgdatra();
            dr1 = dt1.Rows[0];
            dr2 = dt2.Rows[0];
            sosachmuon = Convert.ToInt16(dr1[0].ToString()) - Convert.ToInt16(dr2[0].ToString());
            if (sosachmuon + dgvMuonSach.RowCount > frmQuyDinh.sosachmuontoida)
            {
                MessageBox.Show("Số Sách Bạn Đang Mượn Nhiều Hơn So Với Quy Định");
            }
            else
            {
                if (txtMaDG.Text != "")
                {
                    laythongtindocgia(ref info);
                    dt = control.chomuonsach(info);
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("Không tồn tại mã độc giả này");
                    }
                    else
                    {
                        DataRow dr = dt.Rows[0];
                        txtHoTen.Text = dr["HOTEN"].ToString();
                        txtDiaChi.Text = dr["DIACHI"].ToString();
                        txtTrangThai.Text = dr["TRANGTHAI"].ToString();
                        txtLoaiThe.Text = dr["MALOAITHE"].ToString();
                        if ((txtTrangThai.Text == "Vi Pham") || (txtLoaiThe.Text == "MT01"))
                        {
                            btn_chomuon.Enabled = false;
                            MessageBox.Show("Loại thẻ không được phép mượn");
                        }
                        else
                            btn_chomuon.Enabled = true;
                    }
                }
                else
                {
                    MessageBox.Show("Nhập mã độc giả");
                }
                madg = txtMaDG.Text;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            sosachmuon = dgvMuonSach.RowCount;
            PhieumuonControl pmcontrol = new PhieumuonControl();
            PhieumuonInfo pminfo = new PhieumuonInfo();
            CTPMinfo ctinfo = new CTPMinfo();
            CTPMcontrol ctcontrol = new CTPMcontrol();
            SachControl scontrol = new SachControl();
            SachInfo sinfo = new SachInfo();
            DataTable dt = new DataTable();
            pminfo.Ngmuon = DateTime.Now;
            pminfo.Ngaytraqd = DateTime.Now.AddDays(10);
            pmcontrol.insertphieumuon(pminfo);
            dt = pmcontrol.sachchoctpm();
            DataRow dr = dt.Rows[0];
            ctinfo.Mapm = dr["MAPMS"].ToString();
            for (int i = 0; i < dgvMuonSach.RowCount; i++)
            {
                ctinfo.Masach = dgvMuonSach.Rows[i].Cells["MASACH"].Value.ToString();
                sinfo.Masach = dgvMuonSach.Rows[i].Cells["MASACH"].Value.ToString();
                ctinfo.Ngaytra = DateTime.Now;
                ctcontrol.insertctpm(ctinfo);
                scontrol.updatemuonsach(sinfo);
            }
            MessageBox.Show("Mượn Sách Thành Công !");
            this.Close();
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }      
    }
}
